1 /*
2  * This file is part of gtkD.
3  *
4  * gtkD is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License
6  * as published by the Free Software Foundation; either version 3
7  * of the License, or (at your option) any later version, with
8  * some exceptions, please read the COPYING file.
9  *
10  * gtkD is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with gtkD; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
18  */
19 
20 // generated automatically - do not change
21 // find conversion definition on APILookup.txt
22 // implement new conversion functionalities on the wrap.utils pakage
23 
24 
25 module gobject.Closure;
26 
27 private import glib.ConstructionException;
28 private import glib.Source;
29 private import gobject.ObjectG;
30 private import gobject.Value;
31 private import gobject.c.functions;
32 public  import gobject.c.types;
33 private import linker.Loader;
34 
35 
36 /**
37  * A #GClosure represents a callback supplied by the programmer.
38  * 
39  * It will generally comprise a function of some kind and a marshaller
40  * used to call it. It is the responsibility of the marshaller to
41  * convert the arguments for the invocation from #GValues into
42  * a suitable form, perform the callback on the converted arguments,
43  * and transform the return value back into a #GValue.
44  * 
45  * In the case of C programs, a closure usually just holds a pointer
46  * to a function and maybe a data argument, and the marshaller
47  * converts between #GValue and native C types. The GObject
48  * library provides the #GCClosure type for this purpose. Bindings for
49  * other languages need marshallers which convert between #GValues
50  * and suitable representations in the runtime of the language in
51  * order to use functions written in that language as callbacks. Use
52  * g_closure_set_marshal() to set the marshaller on such a custom
53  * closure implementation.
54  * 
55  * Within GObject, closures play an important role in the
56  * implementation of signals. When a signal is registered, the
57  * @c_marshaller argument to g_signal_new() specifies the default C
58  * marshaller for any closure which is connected to this
59  * signal. GObject provides a number of C marshallers for this
60  * purpose, see the g_cclosure_marshal_*() functions. Additional C
61  * marshallers can be generated with the [glib-genmarshal][glib-genmarshal]
62  * utility.  Closures can be explicitly connected to signals with
63  * g_signal_connect_closure(), but it usually more convenient to let
64  * GObject create a closure automatically by using one of the
65  * g_signal_connect_*() functions which take a callback function/user
66  * data pair.
67  * 
68  * Using closures has a number of important advantages over a simple
69  * callback function/data pointer combination:
70  * 
71  * - Closures allow the callee to get the types of the callback parameters,
72  * which means that language bindings don't have to write individual glue
73  * for each callback type.
74  * 
75  * - The reference counting of #GClosure makes it easy to handle reentrancy
76  * right; if a callback is removed while it is being invoked, the closure
77  * and its parameters won't be freed until the invocation finishes.
78  * 
79  * - g_closure_invalidate() and invalidation notifiers allow callbacks to be
80  * automatically removed when the objects they point to go away.
81  */
82 public class Closure
83 {
84 	/** the main Gtk struct */
85 	protected GClosure* gClosure;
86 	protected bool ownedRef;
87 
88 	/** Get the main Gtk struct */
89 	public GClosure* getClosureStruct(bool transferOwnership = false)
90 	{
91 		if (transferOwnership)
92 			ownedRef = false;
93 		return gClosure;
94 	}
95 
96 	/** the main Gtk struct as a void* */
97 	protected void* getStruct()
98 	{
99 		return cast(void*)gClosure;
100 	}
101 
102 	/**
103 	 * Sets our main struct and passes it to the parent class.
104 	 */
105 	public this (GClosure* gClosure, bool ownedRef = false)
106 	{
107 		this.gClosure = gClosure;
108 		this.ownedRef = ownedRef;
109 	}
110 
111 	~this ()
112 	{
113 		if ( Linker.isLoaded(LIBRARY_GOBJECT[0]) && ownedRef )
114 			g_closure_unref(gClosure);
115 	}
116 
117 
118 	/** */
119 	public static GType getType()
120 	{
121 		return g_closure_get_type();
122 	}
123 
124 	/**
125 	 * A variant of g_closure_new_simple() which stores @object in the
126 	 * @data field of the closure and calls g_object_watch_closure() on
127 	 * @object and the created closure. This function is mainly useful
128 	 * when implementing new types of closures.
129 	 *
130 	 * Params:
131 	 *     sizeofClosure = the size of the structure to allocate, must be at least
132 	 *         `sizeof (GClosure)`
133 	 *     object = a #GObject pointer to store in the @data field of the newly
134 	 *         allocated #GClosure
135 	 *
136 	 * Returns: a newly allocated #GClosure
137 	 *
138 	 * Throws: ConstructionException GTK+ fails to create the object.
139 	 */
140 	public this(uint sizeofClosure, ObjectG object)
141 	{
142 		auto __p = g_closure_new_object(sizeofClosure, (object is null) ? null : object.getObjectGStruct());
143 
144 		if(__p is null)
145 		{
146 			throw new ConstructionException("null returned by new_object");
147 		}
148 
149 		this(cast(GClosure*) __p);
150 	}
151 
152 	/**
153 	 * Allocates a struct of the given size and initializes the initial
154 	 * part as a #GClosure.
155 	 *
156 	 * This function is mainly useful when implementing new types of closures:
157 	 *
158 	 * |[<!-- language="C" -->
159 	 * typedef struct _MyClosure MyClosure;
160 	 * struct _MyClosure
161 	 * {
162 	 * GClosure closure;
163 	 * // extra data goes here
164 	 * };
165 	 *
166 	 * static void
167 	 * my_closure_finalize (gpointer  notify_data,
168 	 * GClosure *closure)
169 	 * {
170 	 * MyClosure *my_closure = (MyClosure *)closure;
171 	 *
172 	 * // free extra data here
173 	 * }
174 	 *
175 	 * MyClosure *my_closure_new (gpointer data)
176 	 * {
177 	 * GClosure *closure;
178 	 * MyClosure *my_closure;
179 	 *
180 	 * closure = g_closure_new_simple (sizeof (MyClosure), data);
181 	 * my_closure = (MyClosure *) closure;
182 	 *
183 	 * // initialize extra data here
184 	 *
185 	 * g_closure_add_finalize_notifier (closure, notify_data,
186 	 * my_closure_finalize);
187 	 * return my_closure;
188 	 * }
189 	 * ]|
190 	 *
191 	 * Params:
192 	 *     sizeofClosure = the size of the structure to allocate, must be at least
193 	 *         `sizeof (GClosure)`
194 	 *     data = data to store in the @data field of the newly allocated #GClosure
195 	 *
196 	 * Returns: a floating reference to a new #GClosure
197 	 *
198 	 * Throws: ConstructionException GTK+ fails to create the object.
199 	 */
200 	public this(uint sizeofClosure, void* data)
201 	{
202 		auto __p = g_closure_new_simple(sizeofClosure, data);
203 
204 		if(__p is null)
205 		{
206 			throw new ConstructionException("null returned by new_simple");
207 		}
208 
209 		this(cast(GClosure*) __p);
210 	}
211 
212 	/**
213 	 * Registers a finalization notifier which will be called when the
214 	 * reference count of @closure goes down to 0.
215 	 *
216 	 * Multiple finalization notifiers on a single closure are invoked in
217 	 * unspecified order. If a single call to g_closure_unref() results in
218 	 * the closure being both invalidated and finalized, then the invalidate
219 	 * notifiers will be run before the finalize notifiers.
220 	 *
221 	 * Params:
222 	 *     notifyData = data to pass to @notify_func
223 	 *     notifyFunc = the callback function to register
224 	 */
225 	public void addFinalizeNotifier(void* notifyData, GClosureNotify notifyFunc)
226 	{
227 		g_closure_add_finalize_notifier(gClosure, notifyData, notifyFunc);
228 	}
229 
230 	/**
231 	 * Registers an invalidation notifier which will be called when the
232 	 * @closure is invalidated with g_closure_invalidate().
233 	 *
234 	 * Invalidation notifiers are invoked before finalization notifiers,
235 	 * in an unspecified order.
236 	 *
237 	 * Params:
238 	 *     notifyData = data to pass to @notify_func
239 	 *     notifyFunc = the callback function to register
240 	 */
241 	public void addInvalidateNotifier(void* notifyData, GClosureNotify notifyFunc)
242 	{
243 		g_closure_add_invalidate_notifier(gClosure, notifyData, notifyFunc);
244 	}
245 
246 	/**
247 	 * Adds a pair of notifiers which get invoked before and after the
248 	 * closure callback, respectively.
249 	 *
250 	 * This is typically used to protect the extra arguments for the
251 	 * duration of the callback. See g_object_watch_closure() for an
252 	 * example of marshal guards.
253 	 *
254 	 * Params:
255 	 *     preMarshalData = data to pass
256 	 *         to @pre_marshal_notify
257 	 *     preMarshalNotify = a function to call before the closure callback
258 	 *     postMarshalData = data to pass
259 	 *         to @post_marshal_notify
260 	 *     postMarshalNotify = a function to call after the closure callback
261 	 */
262 	public void addMarshalGuards(void* preMarshalData, GClosureNotify preMarshalNotify, void* postMarshalData, GClosureNotify postMarshalNotify)
263 	{
264 		g_closure_add_marshal_guards(gClosure, preMarshalData, preMarshalNotify, postMarshalData, postMarshalNotify);
265 	}
266 
267 	/**
268 	 * Sets a flag on the closure to indicate that its calling
269 	 * environment has become invalid, and thus causes any future
270 	 * invocations of g_closure_invoke() on this @closure to be
271 	 * ignored.
272 	 *
273 	 * Also, invalidation notifiers installed on the closure will
274 	 * be called at this point. Note that unless you are holding a
275 	 * reference to the closure yourself, the invalidation notifiers may
276 	 * unref the closure and cause it to be destroyed, so if you need to
277 	 * access the closure after calling g_closure_invalidate(), make sure
278 	 * that you've previously called g_closure_ref().
279 	 *
280 	 * Note that g_closure_invalidate() will also be called when the
281 	 * reference count of a closure drops to zero (unless it has already
282 	 * been invalidated before).
283 	 */
284 	public void invalidate()
285 	{
286 		g_closure_invalidate(gClosure);
287 	}
288 
289 	/**
290 	 * Invokes the closure, i.e. executes the callback represented by the @closure.
291 	 *
292 	 * Params:
293 	 *     returnValue = a #GValue to store the return
294 	 *         value. May be %NULL if the callback of @closure
295 	 *         doesn't return a value.
296 	 *     paramValues = an array of
297 	 *         #GValues holding the arguments on which to
298 	 *         invoke the callback of @closure
299 	 *     invocationHint = a context-dependent invocation hint
300 	 */
301 	public void invoke(ref Value returnValue, Value[] paramValues, void* invocationHint)
302 	{
303 		GValue[] paramValuesArray = new GValue[paramValues.length];
304 		for ( int i = 0; i < paramValues.length; i++ )
305 		{
306 			paramValuesArray[i] = *(paramValues[i].getValueStruct());
307 		}
308 
309 		g_closure_invoke(gClosure, (returnValue is null) ? null : returnValue.getValueStruct(), cast(uint)paramValues.length, paramValuesArray.ptr, invocationHint);
310 	}
311 
312 	alias doref = ref_;
313 	/**
314 	 * Increments the reference count on a closure to force it staying
315 	 * alive while the caller holds a pointer to it.
316 	 *
317 	 * Returns: The @closure passed in, for convenience
318 	 */
319 	public Closure ref_()
320 	{
321 		auto __p = g_closure_ref(gClosure);
322 
323 		if(__p is null)
324 		{
325 			return null;
326 		}
327 
328 		return ObjectG.getDObject!(Closure)(cast(GClosure*) __p);
329 	}
330 
331 	/**
332 	 * Removes a finalization notifier.
333 	 *
334 	 * Notice that notifiers are automatically removed after they are run.
335 	 *
336 	 * Params:
337 	 *     notifyData = data which was passed to g_closure_add_finalize_notifier()
338 	 *         when registering @notify_func
339 	 *     notifyFunc = the callback function to remove
340 	 */
341 	public void removeFinalizeNotifier(void* notifyData, GClosureNotify notifyFunc)
342 	{
343 		g_closure_remove_finalize_notifier(gClosure, notifyData, notifyFunc);
344 	}
345 
346 	/**
347 	 * Removes an invalidation notifier.
348 	 *
349 	 * Notice that notifiers are automatically removed after they are run.
350 	 *
351 	 * Params:
352 	 *     notifyData = data which was passed to g_closure_add_invalidate_notifier()
353 	 *         when registering @notify_func
354 	 *     notifyFunc = the callback function to remove
355 	 */
356 	public void removeInvalidateNotifier(void* notifyData, GClosureNotify notifyFunc)
357 	{
358 		g_closure_remove_invalidate_notifier(gClosure, notifyData, notifyFunc);
359 	}
360 
361 	/**
362 	 * Sets the marshaller of @closure.
363 	 *
364 	 * The `marshal_data` of @marshal provides a way for a meta marshaller to
365 	 * provide additional information to the marshaller.
366 	 *
367 	 * For GObject's C predefined marshallers (the `g_cclosure_marshal_*()`
368 	 * functions), what it provides is a callback function to use instead of
369 	 * @closure->callback.
370 	 *
371 	 * See also: g_closure_set_meta_marshal()
372 	 *
373 	 * Params:
374 	 *     marshal = a #GClosureMarshal function
375 	 */
376 	public void setMarshal(GClosureMarshal marshal)
377 	{
378 		g_closure_set_marshal(gClosure, marshal);
379 	}
380 
381 	/**
382 	 * Sets the meta marshaller of @closure.
383 	 *
384 	 * A meta marshaller wraps the @closure's marshal and modifies the way
385 	 * it is called in some fashion. The most common use of this facility
386 	 * is for C callbacks.
387 	 *
388 	 * The same marshallers (generated by [glib-genmarshal][glib-genmarshal]),
389 	 * are used everywhere, but the way that we get the callback function
390 	 * differs. In most cases we want to use the @closure's callback, but in
391 	 * other cases we want to use some different technique to retrieve the
392 	 * callback function.
393 	 *
394 	 * For example, class closures for signals (see
395 	 * g_signal_type_cclosure_new()) retrieve the callback function from a
396 	 * fixed offset in the class structure.  The meta marshaller retrieves
397 	 * the right callback and passes it to the marshaller as the
398 	 * @marshal_data argument.
399 	 *
400 	 * Params:
401 	 *     marshalData = context-dependent data to pass
402 	 *         to @meta_marshal
403 	 *     metaMarshal = a #GClosureMarshal function
404 	 */
405 	public void setMetaMarshal(void* marshalData, GClosureMarshal metaMarshal)
406 	{
407 		g_closure_set_meta_marshal(gClosure, marshalData, metaMarshal);
408 	}
409 
410 	/**
411 	 * Takes over the initial ownership of a closure.
412 	 *
413 	 * Each closure is initially created in a "floating" state, which means
414 	 * that the initial reference count is not owned by any caller.
415 	 *
416 	 * This function checks to see if the object is still floating, and if so,
417 	 * unsets the floating state and decreases the reference count. If the
418 	 * closure is not floating, g_closure_sink() does nothing.
419 	 *
420 	 * The reason for the existence of the floating state is to prevent
421 	 * cumbersome code sequences like:
422 	 *
423 	 * |[<!-- language="C" -->
424 	 * closure = g_cclosure_new (cb_func, cb_data);
425 	 * g_source_set_closure (source, closure);
426 	 * g_closure_unref (closure); // GObject doesn't really need this
427 	 * ]|
428 	 *
429 	 * Because g_source_set_closure() (and similar functions) take ownership of the
430 	 * initial reference count, if it is unowned, we instead can write:
431 	 *
432 	 * |[<!-- language="C" -->
433 	 * g_source_set_closure (source, g_cclosure_new (cb_func, cb_data));
434 	 * ]|
435 	 *
436 	 * Generally, this function is used together with g_closure_ref(). An example
437 	 * of storing a closure for later notification looks like:
438 	 *
439 	 * |[<!-- language="C" -->
440 	 * static GClosure *notify_closure = NULL;
441 	 * void
442 	 * foo_notify_set_closure (GClosure *closure)
443 	 * {
444 	 * if (notify_closure)
445 	 * g_closure_unref (notify_closure);
446 	 * notify_closure = closure;
447 	 * if (notify_closure)
448 	 * {
449 	 * g_closure_ref (notify_closure);
450 	 * g_closure_sink (notify_closure);
451 	 * }
452 	 * }
453 	 * ]|
454 	 *
455 	 * Because g_closure_sink() may decrement the reference count of a closure
456 	 * (if it hasn't been called on @closure yet) just like g_closure_unref(),
457 	 * g_closure_ref() should be called prior to this function.
458 	 */
459 	public void sink()
460 	{
461 		g_closure_sink(gClosure);
462 	}
463 
464 	/**
465 	 * Decrements the reference count of a closure after it was previously
466 	 * incremented by the same caller.
467 	 *
468 	 * If no other callers are using the closure, then the closure will be
469 	 * destroyed and freed.
470 	 */
471 	public void unref()
472 	{
473 		g_closure_unref(gClosure);
474 	}
475 
476 	/**
477 	 * Set the callback for a source as a #GClosure.
478 	 *
479 	 * If the source is not one of the standard GLib types, the @closure_callback
480 	 * and @closure_marshal fields of the #GSourceFuncs structure must have been
481 	 * filled in with pointers to appropriate functions.
482 	 *
483 	 * Params:
484 	 *     source = the source
485 	 *     closure = a #GClosure
486 	 */
487 	public static void sourceSetClosure(Source source, Closure closure)
488 	{
489 		g_source_set_closure((source is null) ? null : source.getSourceStruct(), (closure is null) ? null : closure.getClosureStruct());
490 	}
491 
492 	/**
493 	 * Sets a dummy callback for @source. The callback will do nothing, and
494 	 * if the source expects a #gboolean return value, it will return %TRUE.
495 	 * (If the source expects any other type of return value, it will return
496 	 * a 0/%NULL value; whatever g_value_init() initializes a #GValue to for
497 	 * that type.)
498 	 *
499 	 * If the source is not one of the standard GLib types, the
500 	 * @closure_callback and @closure_marshal fields of the #GSourceFuncs
501 	 * structure must have been filled in with pointers to appropriate
502 	 * functions.
503 	 *
504 	 * Params:
505 	 *     source = the source
506 	 */
507 	public static void sourceSetDummyCallback(Source source)
508 	{
509 		g_source_set_dummy_callback((source is null) ? null : source.getSourceStruct());
510 	}
511 }